// pages/distribution/order.js
const app = getApp()
const pool = new Map()
const distributionTabs = [{
        name: '全部订单',
        status: '-100'
    },
    {
        name: '待支付',
        status: '0'
    },
    {
        name: '已支付',
        status: '12'
    },
    {
        name: '已完成',
        status: '100'
    },
]
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: distributionTabs[0].status,
        distributionTabs,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        pool.clear()
        this.loadData()
    },

    loadData() {
        let tab = pool.get(this.data.type)
        if (!tab) {
            tab = {
                page: 1,
                nextPage: true,
                list: [],
                loading:false
            }
            pool.set(this.data.type, tab)
        }
        if (!tab.nextPage) {
            this.setData(tab)
            return
        }
        wx.showLoading({
            title: '加载中...',
        })
        this.setData({
            loading: true
        })
        app.http.post("distributor/order?page=" + tab.page, {
            data: {
                status: this.data.type,
            }
        }).then(res => {
            tab.list.push(...res.data.list)
            tab.page++
            tab.nextPage = res.data.nextPage
            this.setData(tab)
            wx.hideLoading()
            wx.stopPullDownRefresh()
            this.setData({
                loading: false
            })
        }).catch(err => {
            wx.hideLoading()
            this.setData({
                loading: false
            })
        })
    },

    typeClick(evt) {
        const type = evt.currentTarget.dataset.status
        if (this.data.type != type) {
            this.setData({
                type
            })
            this.loadData()
        }
    },
    onReachBottom() {
        this.loadData()
    },
    onPullDownRefresh() {
        pool.set(this.data.type, null)
        this.loadData()
    }
})